package minh.bac.wcbrazil2014.netsupport;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import minh.bac.wcbrazil2014.object.FootBallTeamObj;
import minh.bac.wcbrazil2014.object.MatchScheduleObj;
import minh.bac.wcbrazil2014.object.VongBangObj;
import minh.bac.wcbrazil2014.object.VongDau;
import minh.bac.wcbrazil2014.object.XepHangObj;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JsonParser {

	/*
	 * this function get arraylist foootball from json object
	 */
	public static ArrayList<FootBallTeamObj> getThongTinDoiBong(String res)
			throws JSONException {
		ArrayList<FootBallTeamObj> arrayListDes = new ArrayList<FootBallTeamObj>();
		ArrayList<FootBallTeamObj> arrayListTemp;
		JSONObject jsonRootObject = new JSONObject(res);
		//
		// get
		JSONArray jsonDetailTeam = jsonRootObject
				.getJSONArray("thongtindoibong");
		JSONObject jsonObject = jsonDetailTeam.getJSONObject(0);
		//
		String[] bangDau = { "banga", "bangb", "bangc", "bangd", "bange",
				"bangf", "bangg", "bangh" };
		JSONArray arrayListBang;
		// ----------------------------------
		for (String bang : bangDau) {
			arrayListTemp = new ArrayList<FootBallTeamObj>();
			arrayListBang = new JSONArray();
			arrayListBang = jsonObject.getJSONArray(bang);
			arrayListTemp = new ArrayList<FootBallTeamObj>();
			for (int i = 0; i < arrayListBang.length(); i++) {

				String doiBong = "", thuMon = "", hauVe = "", tienVe = "", tienDao = "", tuCachThamDu = "", xepHangFiFa = "", lichSuThamDu = "";
				JSONObject item = arrayListBang.getJSONObject(i);
				// add to temp list footballteam
				doiBong = item.getString("doibong");
				thuMon = item.getString("thumon");
				hauVe = item.getString("hauve");
				tienVe = item.getString("tienve");
				tienDao = item.getString("tiendao");
				tuCachThamDu = item.getString("tucachthamdu");
				xepHangFiFa = item.getString("xephangfifa");
				lichSuThamDu = item.getString("lichsuthamdu");
				//
				FootBallTeamObj ballTeamObj = new FootBallTeamObj(doiBong,
						bang, thuMon, hauVe, tienVe, tienDao, tuCachThamDu,
						xepHangFiFa, lichSuThamDu, 0);
				arrayListTemp.add(ballTeamObj);
			}
			arrayListDes.addAll(arrayListTemp);
		}
		//
		return arrayListDes;
	}

	public static ArrayList<XepHangObj> getXepHangBang(String res)
			throws JSONException {

		ArrayList<XepHangObj> arrayListXepHang = new ArrayList<XepHangObj>();
		// ArrayList<XepHangObj> arrayListTemp;
		JSONObject rootJsonObject = new JSONObject(res);

		JSONArray jsonArrayXephang = rootJsonObject.getJSONArray("bangxephang");
		// JSONObject jsonObject = jsonArrayXephang.getJSONObject(0);
		//
		for (int i = 0; i < jsonArrayXephang.length(); i++) {
			JSONObject item = new JSONObject();
			item = jsonArrayXephang.getJSONObject(i);
			String bang = "", doibong = "", image = "", tran = "", thb = "", hieuso = "", diem = "";
			// JSONObject item = jsonArrayListXepHang.getJSONObject(i);
			bang = item.getString("bangdau");
			doibong = item.getString("doibong");
			image = item.getString("image");
			Log.d("JsonP", "json position " + i);
			tran = item.getString("trandau");
			thb = item.getString("thb");
			hieuso = item.getString("hieuso");
			diem = item.getString("diem");

			XepHangObj xepHangObj = new XepHangObj(bang, doibong, image, tran,
					thb, hieuso, diem);
			arrayListXepHang.add(xepHangObj);
		}
		//
		//
		// String[] XepHang = { "bangxephang" };
		// JSONArray jsonArrayListXepHang;
		//
		// for (String xephang : XepHang) {
		// arrayListTemp = new ArrayList<XepHangObj>();
		// jsonArrayListXepHang = new JSONArray();
		// jsonArrayListXepHang = jsonObject.getJSONArray(xephang);
		//
		// for (int i = 0; i < jsonArrayListXepHang.length(); i++) {
		// String doibong = "", image = "", tran = "", thb = "", hieuso = "",
		// diem = "";
		// JSONObject item = jsonArrayListXepHang.getJSONObject(i);
		// // bang = item.getString("bangdau");
		// doibong = item.getString("doibong");
		// image = item.getString("image");
		// tran = item.getString("trandau");
		// thb = item.getString("thb");
		// hieuso = item.getString("hieuso");
		// diem = item.getString("diem");
		//
		// XepHangObj xepHangObj = new XepHangObj(doibong, image, tran,
		// thb, hieuso, diem);
		// arrayListTemp.add(xepHangObj);
		// }
		// arrayListXepHang.addAll(arrayListTemp);
		// }

		return arrayListXepHang;
	}

	/*
	 * this function get matchSchedule from json
	 */
	public static LinkedHashMap<String, ArrayList<MatchScheduleObj>> getLichDauVongBang(
			String res) throws JSONException {
		LinkedHashMap<String, ArrayList<MatchScheduleObj>> mapDes = new LinkedHashMap<String, ArrayList<MatchScheduleObj>>();
		ArrayList<MatchScheduleObj> tempList;
		String[] vongDau = { "vongbang", "vong1/8", "vongtuket", "vongbanket",
				"giai3,4", "chungket" };
		JSONObject rootJsonObject = new JSONObject(res);
		JSONArray jsonArray;
		for (String item : vongDau) {
			jsonArray = new JSONArray();
			jsonArray = rootJsonObject.getJSONArray(item);
			if (item.equals("vongbang")) {
				tempList = new ArrayList<MatchScheduleObj>();
				String tran = "", ngay = "", gio = "", doi1 = "", imgLinkDoi1 = "", score1 = "", doi2 = "", imgLinkdoi2 = "", score2 = "", diaDiem = "", kenhTV = "", bang = "";

				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject itemArray = jsonArray.getJSONObject(i);
					//
					bang = itemArray.getString("bang");
					tran = itemArray.getString("tran");
					ngay = itemArray.getString("ngay");
					String[] tempS = ngay.split(",");
					ngay = tempS[1].trim() + "," + tempS[0];
					gio = itemArray.getString("gio");
					doi1 = itemArray.getString("doi1");
					imgLinkDoi1 = itemArray.getString("img1");
					score1 = itemArray.getString("score1");
					doi2 = itemArray.getString("doi2");
					imgLinkdoi2 = itemArray.getString("img2");
					score2 = itemArray.getString("score2");
					diaDiem = itemArray.getString("diadiem");
					kenhTV = itemArray.getString("kenh");
					//
					String[] check = { bang, tran, ngay, gio, doi1,
							imgLinkDoi1, score1, imgLinkdoi2, score2, diaDiem,
							kenhTV };
					checkNull(check);
					MatchScheduleObj temp = new VongBangObj(tran, ngay, gio,
							doi1, imgLinkDoi1, score1, doi2, imgLinkdoi2,
							score2, diaDiem, kenhTV, bang);
					tempList.add(temp);
				}
				mapDes.put(item, tempList);

			} else {
				tempList = new ArrayList<MatchScheduleObj>();
				String tran = "", ngay = "", gio = "", doi1 = "", imgLinkDoi1 = "", score1 = "", doi2 = "", imgLinkdoi2 = "", score2 = "", diaDiem = "", kenhTV = "", vong = "";
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject itemArray = jsonArray.getJSONObject(i);
					//
					vong = itemArray.getString("vong");
					tran = itemArray.getString("tran");
					ngay = itemArray.getString("ngay");
					String[] tempS = ngay.split(",");
					ngay = tempS[1].trim() + "," + tempS[0];
					gio = itemArray.getString("gio");
					doi1 = itemArray.getString("doi1");
					imgLinkDoi1 = itemArray.getString("img1");
					score1 = itemArray.getString("score1");
					doi2 = itemArray.getString("doi2");
					imgLinkdoi2 = itemArray.getString("img2");
					score2 = itemArray.getString("score2");
					diaDiem = itemArray.getString("diadiem");
					kenhTV = itemArray.getString("kenh");
					//
					String[] check = { vong, tran, ngay, gio, doi1,
							imgLinkDoi1, score1, imgLinkdoi2, score2, diaDiem,
							kenhTV };
					checkNull(check);
					MatchScheduleObj temp = new VongDau(tran, ngay, gio, doi1,
							imgLinkDoi1, score1, doi2, imgLinkdoi2, score2,
							diaDiem, kenhTV, vong);
					tempList.add(temp);
				}
				mapDes.put(item, tempList);
			}

		}
		return mapDes;
	}

	// public static LinkedHashMap<String, ArrayList<XepHangObj>>
	// getXepHangVongBang(
	// String res) throws JSONException {
	// LinkedHashMap<String, ArrayList<XepHangObj>> mapDes = new
	// LinkedHashMap<String, ArrayList<XepHangObj>>();
	// ArrayList<XepHangObj> tempList;
	// String[] bangxephang = { "bangxephang" };
	// JSONObject jsonObject = new JSONObject(res);
	// JSONArray jsonArray;
	// for (String item : bangxephang) {
	// jsonArray = new JSONArray();
	// jsonArray = jsonObject.getJSONArray(item);
	// if (item.equals("bangxephang")) {
	// tempList = new ArrayList<XepHangObj>();
	// String doibong = "", image = "", tran = "", thb = "", hieuso = "", diem =
	// "", bangdau = "";
	// for (int i = 0; i < jsonArray.length(); i++) {
	// JSONObject itemArray = jsonArray.getJSONObject(i);
	// //
	// doibong = itemArray.getString("doibong");
	// image = itemArray.getString("image");
	// tran = itemArray.getString("trandau");
	// thb = itemArray.getString("thb");
	// hieuso = itemArray.getString("hieuso");
	// diem = itemArray.getString("diem");
	// //
	// String[] check = { doibong, image, tran, thb, hieuso, diem };
	// checkNull(check);
	//
	// XepHangObj temp = new XepHang(doibong, image, tran, thb,
	// hieuso, diem, bangdau);
	// tempList.add(temp);
	// }
	// mapDes.put(item, tempList);
	// }
	// }
	//
	// return mapDes;
	// }

	public static void checkNull(String[] res) {
		for (int i = 0; i < res.length; i++) {
			if (res[i] == null) {
				res[i] = "Chưa cập nhật";
			}
		}
	}
}
